//
//  LCVisitorView.swift
//  SinaWeiBo
//
//  Created by mac on 16/12/1.
//  Copyright © 2016年 heima. All rights reserved.
//

import UIKit

class LCVisitorView: UIView {
    
    
    var callBack: (() -> ())?
    
    //懒加载控件
    //旋转背景图片
    private lazy var cycleImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    //遮盖图片
    private lazy var markImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    //房子图片
    private lazy var iconImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    //信息描述
    private lazy var messageLabel: UILabel = {
        let label = UILabel()
        label.numberOfLines = 0
        label.textColor = UIColor.darkGray
        label.font = UIFont.systemFont(ofSize: 13)
        label.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        //设置文字居中
        label.textAlignment = NSTextAlignment.center
        return label
    }()
    //注册button
    private lazy var registerButton: UIButton = {
        let regisButton = UIButton()
        //添加button点击事件
        regisButton .addTarget(self, action: #selector(registerAction), for: .touchUpInside)
        regisButton.setBackgroundImage(UIImage(named:"common_button_white_disable"), for: .normal)
        regisButton.setTitle("注册", for: .normal)
        regisButton.setTitleColor(UIColor.orange, for: .highlighted)
        regisButton.setTitleColor(UIColor.darkGray, for: .normal)
        regisButton.adjustsImageWhenHighlighted = false
        
        return regisButton
        
    }()
    //登录button
    private lazy var logonButton: UIButton = {
        let logButton = UIButton()
        logButton .addTarget(self, action: #selector(logonAction), for: .touchUpInside)
        logButton.setBackgroundImage(UIImage(named:"common_button_white_disable"), for: .normal)
        logButton.setTitle("登录", for: .normal)
        logButton.setTitleColor(UIColor.darkGray, for: .normal)
        logButton.setTitleColor(UIColor.orange, for: .highlighted)
        logButton.adjustsImageWhenHighlighted = false
        return logButton
    }()
    
    //1重写init方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    //2使用xib会提示错误
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI(){
        self.backgroundColor = UIColor(white: 227/255.0, alpha: 1)
        //1将子控件添加到view中
        addSubview(cycleImageView)
        addSubview(markImageView)
        addSubview(iconImageView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(logonButton)
        
        //2设置约束
            //2-1禁用autoresizing
        cycleImageView.translatesAutoresizingMaskIntoConstraints = false
        markImageView.translatesAutoresizingMaskIntoConstraints = false
        iconImageView.translatesAutoresizingMaskIntoConstraints = false
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        registerButton.translatesAutoresizingMaskIntoConstraints = false
        logonButton.translatesAutoresizingMaskIntoConstraints = false
            //2-2设置约束
        //1
        addConstraint(NSLayoutConstraint(item: cycleImageView, attribute: .centerX, relatedBy: .equal, toItem: self, attribute: .centerX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: cycleImageView, attribute: .centerY, relatedBy: .equal, toItem: self, attribute: .centerY, multiplier: 1, constant: 0))
        //2
        addConstraint(NSLayoutConstraint(item: markImageView, attribute: .centerX, relatedBy: .equal, toItem: cycleImageView, attribute: .centerX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: markImageView, attribute: .centerY, relatedBy: .equal, toItem: cycleImageView, attribute: .centerY, multiplier: 1, constant: 0))
        //3
        addConstraint(NSLayoutConstraint(item: iconImageView, attribute: .centerX, relatedBy: .equal, toItem: cycleImageView, attribute: .centerX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconImageView, attribute: .centerY, relatedBy: .equal, toItem: cycleImageView, attribute: .centerY, multiplier: 1, constant: 0))
        //4
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .centerX, relatedBy: .equal, toItem: cycleImageView, attribute: .centerX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .top, relatedBy: .equal, toItem: cycleImageView, attribute: .bottom, multiplier: 1, constant: 10))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.width, relatedBy: .equal, toItem: nil, attribute: NSLayoutAttribute.width, multiplier: 1.0, constant: 224))
        //5
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .leading, relatedBy: .equal, toItem: messageLabel, attribute: .leading, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .top, relatedBy: .equal, toItem: messageLabel, attribute: .bottom, multiplier: 1, constant: 10))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .width, multiplier: 1, constant: 80))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .height, multiplier: 1, constant: 35))
        
        //6
        addConstraint(NSLayoutConstraint(item: logonButton, attribute: .trailing, relatedBy: .equal, toItem: messageLabel, attribute: .trailing, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: logonButton, attribute: .top, relatedBy: .equal, toItem: messageLabel, attribute: .bottom, multiplier: 1, constant: 10))
        addConstraint(NSLayoutConstraint(item: logonButton, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .width, multiplier: 1, constant: 80))
        addConstraint(NSLayoutConstraint(item: logonButton, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .height, multiplier: 1, constant: 35))

    }
    //MARK:创建主页动画
    private func startAnimation(){
        //1创建一个基础核心动画
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        //2旋转的角度
        animation.toValue = 2*M_PI
        //设置动画的属性
        animation.duration = 6;
        animation.repeatCount = MAXFLOAT
        
        //MARK:设置动画失去焦点以后不停止
        animation.isRemovedOnCompletion = false
        
        //3把动画添加到需要旋转view的layer上
        cycleImageView.layer .add(animation, forKey: "rotation")
    }
    //MARK:设置各个控制器的访客视图
    func updateVisitorView(imageName: String?,message: String?){
        //1判断传入的imageName和Message如果都有值的话，那么控制器是消息、发现、我的这三个控制器
        if let image = imageName,let mesg = message{
            iconImageView.image = UIImage(named: image)
            messageLabel.text = mesg
            cycleImageView.isHidden = true
        }else{
            //开启主页的动画效果
            startAnimation()
        }
        
    }
    //MARK:button的点击事件
    func registerAction(){
        callBack?()
    }
    func logonAction(){
        callBack?()
    }
    
    
    
}
